﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Idlesoft.Utils
{
	public static class FileExtensions
	{
		#region read write
		public static string ReadText(this string file)
		{
			return File.Exists(file) ? File.ReadAllText(file) : null;
		}
		public static string[] ReadLines(this string file)
		{
			return File.Exists(file) ? File.ReadAllLines(file) : null;
		}
		public static void WriteText(this string text, string file)
		{
			File.WriteAllText(file, text);
		}
		public static void WriteLines(this string[] lines, string file)
		{
			File.WriteAllLines(file, lines);
		}
		//public static void WriteText(this string file, string text)
		//{
		//    File.WriteAllText(file, text);
		//}
		//public static void WriteLines(this string file, params string[] lines)
		//{
		//    File.WriteAllLines(file, lines);
		//}
		//public static string FileToText(this string file)
		//{
		//    return File.Exists(file) ? File.ReadAllText(file) : null;
		//}
		//public static string FileToLines(this string file)
		//{
		//    return File.Exists(file) ? File.ReadAllLines(file);
		//}
		#endregion read write

		public static bool HasExt(this string file, string ext)
		{
			return Path.GetExtension(file).Equals(ext);
		}

		#region exists
		public static bool FileExists(this string path)
		{
			return File.Exists(path);
		}
		#endregion exists

		#region create
		public static void CreateFile(this string path)
		{
			File.Create(path);
		}
		public static void CreateFile(this string path, string ext)
		{
			File.Create(Path.ChangeExtension(path, ext));
		}
		#endregion create

		#region find
		public static string[] FindFiles(this string path, string pattern, bool recursive = false)
		{
			return Directory.GetFiles(path, pattern, recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
		}
		public static string[] FindFiles(this string path, Func<string, bool> selector = null)
		{
			string[] files = Directory.GetFiles(path);
			return selector != null ? files.Where(selector).ToArray() : files;
		}
		#endregion find

		#region copy
		public static bool CopyFile(this string src, string dst, bool overwrite = true)
		{
			File.Copy(src, dst, overwrite);
			return File.Exists(dst);
		}
		public static int CopyFiles(this string src, string dst, string pattern, bool recursive = false)
		{
			return FindFiles(src, pattern, recursive).Select(file => CopyFile(file, Path.Combine(dst, Path.GetFileName(file)))).Count(b => b);
		}
		public static int CopyFiles(this string src, string dst, Func<string, bool> selector = null)
		{
			return FindFiles(src, selector).Select(file => CopyFile(file, Path.Combine(dst, Path.GetFileName(file)))).Count(b => b);
		}
		#endregion copy

		#region move
		public static bool MoveFile(this string src, string dst)
		{
			File.Move(src, dst);
			return File.Exists(dst);
		}
		public static int MoveFiles(this string src, string dst, string pattern, bool recursive = false)
		{
			return FindFiles(src, pattern, recursive).Select(file => MoveFile(file, Path.Combine(dst, Path.GetFileName(file)))).Count(b => b);
		}
		public static int MoveFiles(this string src, string dst, Func<string, bool> selector = null)
		{
			return FindFiles(src, selector).Select(file => MoveFile(file, Path.Combine(dst, Path.GetFileName(file)))).Count(b => b);
		}
		#endregion move

		#region delete
		public static bool DeleteFile(this string path)
		{
			File.Delete(path);
			return !File.Exists(path);
		}
		public static int DeleteFiles(this string path, string pattern, bool recursive = false)
		{
			return FindFiles(path, pattern, recursive).Select(file => DeleteFile(file)).Count(b => b);
		}
		public static int DeleteFiles(this string path, Func<string, bool> selector = null)
		{
			return FindFiles(path, selector).Select(file => DeleteFile(file)).Count(b => b);
		}
		#endregion delete

		public static string file { get; set; }
	}
}
